//
//  SearchViewController.m
//  EJLX
//
//  Created by 徐绍明 on 15/12/7.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "SearchViewController.h"
#import "PinPaiViewController.h"
#import "ZhiDeSou.h"
@interface SearchViewController ()

@end

@implementation SearchViewController
-(void)viewWillAppear:(BOOL)animated
{
    _resultAry=[[NSMutableArray alloc]init];
    [HttpsManager requestZhiDeSouCompletion:^(NSMutableArray *zhiDeAry)
     {
                 for (int i = 0; i<zhiDeAry.count; i++) {
                     ZhiDeSou *de = zhiDeAry[ i];
                     [_resultAry addObject:de.searchWord];
                 }
         
        [_tableView reloadData];
         
         
     }];

    _liShiAry=[NSMutableArray array];
    NSUserDefaults*user=[NSUserDefaults standardUserDefaults];
    _liShiAry=[user arrayForKey:@"myArray"];
    [_tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    
    [self creatNav];
    [self addSearch];
   

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, WIDE, HEIGHT-120) style:UITableViewStyleGrouped] ;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = RGB(244, 244, 244);
    
    [self.view addSubview:_tableView];
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
  
}
-(void)addSearch{
    UIView *vc = [[UIView alloc] initWithFrame:CGRectMake(0, 66, WIDE, 54)];
    vc.backgroundColor = RGB(240, 240, 240);
    [self.view addSubview:vc];
    UILabel *searchLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, WIDE,34)];
    searchLB.layer.borderWidth = 1;
    searchLB.text = @"  商品 >";
    //searchLB.font=[UIFont systemFontOfSize:14];
    
    searchLB.layer.borderColor = RGB_COLOR(0x00c49f).CGColor;
    searchLB.backgroundColor = [UIColor whiteColor];
    [vc addSubview:searchLB];
    
    searchTF = [[UITextField alloc] initWithFrame:CGRectMake(60, 7, WIDE-120,30)];
    searchTF.backgroundColor = [UIColor whiteColor];
    
    searchTF.placeholder = @"请输入商品名称";
    [vc addSubview:searchTF];
    
    UIButton *searchbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchbtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchbtn addTarget:self action:@selector(searchBtn) forControlEvents:UIControlEventTouchUpInside];
    searchbtn.frame = CGRectMake(WIDE-60, 5, 60,34);
    searchbtn.backgroundColor = RGB_COLOR(0x00c49f);
    
    [vc addSubview:searchbtn];
}

-(void)creatNav{
    
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10,7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60,7, 120, 30  )];
    title.text = @"品牌分销";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
    
}

-(void)backBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)searchBtn{
    
    PinPaiViewController*vc=[[PinPaiViewController alloc]init];
    vc.name=searchTF.text;
    
    NSUserDefaults*user=[NSUserDefaults standardUserDefaults];
    NSArray*myArray=[user arrayForKey:@"myArray"];
    NSMutableArray*array=[myArray mutableCopy];
    if(!array){
        array = [NSMutableArray array];
    }
    //NSMutableArray*array=[[NSMutableArray alloc]init];
    [array addObject:searchTF.text];
    // NSLog(@"000000%lu",(unsigned long)array.count);
    
    
    if (array.count>5)
    {
        [array removeObjectAtIndex:0];
    }
    NSUserDefaults*users=[NSUserDefaults standardUserDefaults];
    [users setObject:[array copy] forKey:@"myArray"];
    
    [user synchronize];

    
    [self.navigationController pushViewController:vc animated:YES];
    }
-(void)readNSUserDefaults
{
//    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
//    //读取数组NSArray类型的数据
//    _liShiAry = [userDefaultes arrayForKey:@"myArray"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return _resultAry.count/4.f;
    }else if (section == 1){
    return _liShiAry.count;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell   = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
       
    }
    if (indexPath.section == 0) {
        for (int i=0; i< 4; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(WIDE/4.f * i, 0, WIDE/4.f, 44);
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = RGB(240, 240, 240).CGColor;
          
            btn.titleLabel.font = [UIFont systemFontOfSize:12.f];
            btn.titleLabel.adjustsFontSizeToFitWidth = YES;
            [btn setTitle:_resultAry[i+4*indexPath.row] forState:UIControlStateNormal];
           
            btn.tag=i+4*indexPath.row;
            [btn addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:btn];
        }
    }else if (indexPath.section == 1){
        cell.textLabel.text = _liShiAry[indexPath.row];
    }
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1)
    {
        PinPaiViewController*vc=[[PinPaiViewController alloc]init];
        vc.name=_liShiAry[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }
}
-(void)butClick:(UIButton*)sender
{
    
    PinPaiViewController*vc=[[PinPaiViewController alloc]init];
    
    vc.name=_resultAry[sender.tag];
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *vc = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDE, 44)];
    vc.backgroundColor = [UIColor whiteColor];
    UIView *smallVC = [[UIView alloc] initWithFrame:CGRectMake(5, 17, 4, 10)];
    smallVC.backgroundColor = RGB_COLOR(0x00c49f);
    [vc addSubview:smallVC];
    
    UILabel *titleLB = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, WIDE-10, vc.frame.size.height)];
    titleLB.text = @[@"值得搜",@"搜索历史"][section];
    [vc addSubview:titleLB];
    
    return vc;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
